package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import flc.ast.bean.ActorBean;
import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.ActorAdapter;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.databinding.ActivityMovieDetailBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class MovieDetailActivity extends BaseAc<ActivityMovieDetailBinding> {

    public static StkResBean stkResBean;
    private boolean isCollect;
    private List<StkResBean> listSp;
    private RecommendAdapter recommendAdapter;
    private ActorAdapter actorAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_movie_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivMovieDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.llDetail.setOnClickListener(this);
        mDataBinding.ivMovieZhui.setOnClickListener(this);

        mDataBinding.rvActorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        actorAdapter = new ActorAdapter();
        mDataBinding.rvActorList.setAdapter(actorAdapter);

        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recommendAdapter = new RecommendAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llDetail://查看解说
                BaseWebviewActivity.open(mContext, stkResBean.getUrl(), stkResBean.getName());
                break;
            case R.id.ivMovieZhui://追剧
                if (isCollect) {
                    listSp.remove(stkResBean);
                    AppSpUtils.saveMovieList(listSp);
                    isCollect = false;
                    mDataBinding.ivMovieZhui.setImageResource(R.drawable.zhuil);
                } else {
                    List<StkResBean> listSave = new ArrayList<>();
                    listSave.add(stkResBean);
                    if (listSp != null && listSp.size() != 0) {
                        listSp.addAll(listSave);
                        AppSpUtils.saveMovieList(listSp);
                    } else {
                        AppSpUtils.saveMovieList(listSave);
                    }
                    isCollect = true;
                    mDataBinding.ivMovieZhui.setImageResource(R.drawable.zhui);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
        finish();
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(stkResBean.getThumbUrl()).into(mDataBinding.ivDetailImg);
        mDataBinding.tvDetailName.setText(stkResBean.getName());

        String str = new Gson().toJson(stkResBean.getExtra());
        String actor = getString(R.string.actor_text2);
        List<ActorBean> list = new ArrayList<>();
        try {
            JSONObject extra = new JSONObject(str);
            mDataBinding.tvDetailDate.setText(extra.optString("releaseDate"));
            JSONArray actorList = extra.optJSONArray("actorList");
            if (actorList != null) {
                for (int i = 0; i < actorList.length(); i++) {
                    JSONObject item = actorList.getJSONObject(i);
                    if (!item.getString("role").equals("导演")) {
                        actor += item.getString("name") + " ";
                        list.add(new ActorBean(item.optString("name"), item.optString("img"), item.optString("role")));
                    }
                }
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        String tag = "";
        for (String s : stkResBean.getTagNameList()) {
            tag += s + " / ";
        }

        mDataBinding.tvDetailType.setText(tag);

        mDataBinding.tvDetailActor.setText(actor);

        mDataBinding.tvDetailDesc.setText(stkResBean.getDesc());

        actorAdapter.setList(list);

        setZhuiData();
        getData();
    }

    //收藏
    private void setZhuiData() {
        listSp = AppSpUtils.getMovieList();
        isCollect = false;
        mDataBinding.ivMovieZhui.setImageResource(R.drawable.zhuil);
        if (listSp != null && listSp.size() != 0) {
            for (StkResBean bean : listSp) {
                if (bean.getUrl().equals(stkResBean.getUrl())) {
                    isCollect = true;
                    mDataBinding.ivMovieZhui.setImageResource(R.drawable.zhui);
                    break;
                }
            }
        }
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.RECOMMEND_DATA, StkResApi.createParamMap(0, 9), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recommendAdapter.setList(result);
                    }
                }
            }
        });
    }

}